import pandas as pd
import logging
from typing import Dict, Any
from config import DATA_DIR
import os

logger = logging.getLogger(__name__)

class DataLoader:
    def __init__(self):
        pass
        
    def load_data(self, file_path: str) -> pd.DataFrame:
        """加载数据文件"""
        try:
            # 如果文件路径不是绝对路径，则相对于DATA_DIR
            if not os.path.isabs(file_path):
                file_path = os.path.join(DATA_DIR, file_path)
                
            if not os.path.exists(file_path):
                raise FileNotFoundError(f"文件不存在: {file_path}")
                
            if file_path.endswith('.csv'):
                return pd.read_csv(file_path)
            elif file_path.endswith('.json'):
                return pd.read_json(file_path)
            else:
                raise ValueError(f"不支持的文件格式: {file_path}")
        except pd.errors.EmptyDataError:
            logger.error(f"文件为空: {file_path}")
            raise
        except Exception as e:
            logger.error(f"加载数据文件失败: {str(e)}")
            raise
            
    # secure02/src2/data_loader.py
    def extract_metadata(self, data: pd.DataFrame) -> Dict[str, Any]:
       try:
         if data.empty:
            raise ValueError("数据为空，无法提取元数据")

         metadata = {
            "columns": list(data.columns),
            "dtypes": {col: str(data[col].dtype) for col in data.columns},
            "row_count": len(data),
            "null_counts": data.isnull().sum().to_dict(),
            "column_stats": {}
        }

         for col in data.columns:
            if pd.api.types.is_numeric_dtype(data[col]):
                metadata["column_stats"][col] = {
                    "min": float(data[col].min()),
                    "max": float(data[col].max()),
                    "mean": float(data[col].mean())
                }
            elif pd.api.types.is_datetime64_any_dtype(data[col]):
                metadata["column_stats"][col] = {
                    "start_date": str(data[col].min()),
                    "end_date": str(data[col].max())
                }

         return metadata
       except Exception as e:
        logger.error(f"提取元数据失败: {str(e)}")
        raise